package com.bjesc_b2c.app.appraiset.adapter;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.listener.OnChangeListener;

import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.CommonAdapter;
import app.pinggushi.bjesc.com.baserecyclerview.adapter.base.ViewHolder;
import master_bean.DemandCarBean;

import static cn.jpush.android.api.JPushInterface.a.o;

/**
 * Created by yidong on 2017/5/23.
 */

public class DemandCarAdapter extends CommonAdapter<DemandCarBean> {
    private OnChangeListener mOnChangeListener;
    List<DemandCarBean> mSelectlist;
    public DemandCarAdapter(Context context, int layoutId, List<DemandCarBean> dataList, OnChangeListener mOnChangeListener) {
        super(context, layoutId, dataList);
        this.mOnChangeListener = mOnChangeListener;
    }

    @Override
    protected void convertView(ViewHolder holder, DemandCarBean demandBean, final int position) {
        if (mSelectlist != null && mSelectlist.size() > 0) {
            if (mSelectlist.get(0).getCar_id().equals(demandBean.getCar_id())) {
                holder.setChecked(R.id.ch_select, true);
            } else {
                holder.setChecked(R.id.ch_select, false);
            }
        } else {
            holder.setChecked(R.id.ch_select, false);
        }

//        if (demandBean.isCheck()){
//            holder.setBackgroundRes(R.id.ch_select, R.mipmap.ic_basic_checkbox_check);
//        holder.setTextColor(R.id.tv_publish_time, R.color.color72aeff);
//        ((CheckBox) holder.getView(R.id.ch_select)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (mOnChangeListener != null) {
//                    mOnChangeListener.OnChangeListener(position, isChecked);
//                }
//            }
//        });
        holder.getView(R.id.ch_select).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnChangeListener.OnChangeListener(position,false);
                    }
                }
        );
//        }else {
//            holder.setBackgroundRes(R.id.iv_selected, R.mipmap.ic_basic_checkbox_notcheck);
//        }
        if (!TextUtils.isEmpty(demandBean.getImg_url())) {
            if (demandBean.getImg_url().contains("http")) {
                holder.setFrescoUrl(R.id.tv_car_photo, demandBean.getImg_url(), true);
            } else {
                holder.setFrescoFile(R.id.tv_car_photo, demandBean.getImg_url(), true);
            }
        }else{
            holder.setFrescoUrl(R.id.tv_car_photo, demandBean.getImg_url(), true);
        }
        if (!TextUtils.isEmpty(demandBean.getDealer_name())) {
            holder.setText(R.id.tv_car_dealers, "车行名称：" + demandBean.getDealer_name());
        }else{
            holder.setText(R.id.tv_car_dealers, "车行名称：" );
        }
        if (!TextUtils.isEmpty(demandBean.getDealer_name())) {
            holder.setText(R.id.tv_market_name, "车市名称：" + demandBean.getMarket_name());
        }else{
            holder.setText(R.id.tv_market_name, "车市名称：" );
        }

        if (!TextUtils.isEmpty(demandBean.getCarspec_name())) {
            holder.setText(R.id.tv_car_type, "车型：" + demandBean.getCarserie_name() + " " + demandBean.getCarspec_name());
        }else{
            holder.setText(R.id.tv_car_type, "车型：" );
        }
        final String type = demandBean.getStatus_type();

        if (!TextUtils.isEmpty(type)) {
            holder.setText(R.id.tv_car_status, "状态：" + type);
        }else{
            holder.setText(R.id.tv_car_status, "状态：");
        }
        if (!TextUtils.isEmpty(demandBean.getOwner_price())) {
            holder.setText(R.id.tv_car_price, demandBean.getOwner_price()+"万");
        }
    }

    public void notifyData(List<DemandCarBean> list) {
        mSelectlist = list;
        Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                notifyDataSetChanged();
            }
        });
    }
}
